package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.Schedule;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 日程表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-07-10
 */
@Mapper
public interface ScheduleMapper extends BaseMapper<Schedule> {

    @Select("<script> " +
            "SELECT a.*, GROUP_CONCAT( b.username ) AS ownerAdminIds FROM " +
            "dragon_schedule a " +
            "INNER JOIN dragon_admin b ON FIND_IN_SET( b.id, a.owner_admin_ids ) " +
            "WHERE 1=1 " +
            "<if test='time != null and time != \"\"'> "+
            " AND DATE_FORMAT( a.start_time, '%Y-%m' ) = #{time} " +
            "</if> " +
            "<if test='search != null and search != \"\"'> "+
            "AND a.title LIKE like CONCAT('%',#{search},'%')" +
            "</if> " +
            " AND a.create_admin_id = #{adminId} " +
            "GROUP BY a.id " +
            "</script>")
    List<Schedule> selectMyPage( Integer adminId, String time, String search);

    @Select("select ds.*, da.username as createAdminName from dragon_schedule ds " +
            "left join dragon_admin da on ds.create_admin_id = da.id where ds.id = #{id} ")
    Schedule selectDetail(Integer id);

    /**
     * 未执行的日程分页
     * @param schedulePage
     * @param wrapper
     * @return
     */
    @Select("SELECT M.* FROM(SELECT ds.*, da.username FROM dragon_schedule ds " +
            "LEFT JOIN dragon_admin da on ds.create_admin_id = da.id) M " +
            "${ew.customSqlSegment} ")
    Page<Schedule> selectScheduleNotExpiredPage(Page<Schedule> schedulePage, @Param(Constants.WRAPPER) QueryWrapper<Schedule> wrapper);

    /**
     * 日程list
     * @param wrapper
     * @return
     */
    @Select("SELECT M.* FROM(SELECT ds.*, da.username FROM dragon_schedule ds " +
            "LEFT JOIN dragon_admin da on ds.create_admin_id = da.id) M " +
            "${ew.customSqlSegment} ")
    List<Schedule> selectListForDay(@Param(Constants.WRAPPER) QueryWrapper<Schedule> wrapper);
}
